package com.shzu.jwc.ty.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shzu.jwc.ty.common.Msg;
import com.shzu.jwc.ty.common.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shzu.jwc.ty.bean.PeExaminationAppointment;
import com.shzu.jwc.ty.common.ExportExcelUtils;
import com.shzu.jwc.ty.common.Semester;
import com.shzu.jwc.ty.service.PeExaminationScoreservice;
import com.shzu.jwc.ty.service.PeExaminationservice;
import com.shzu.jwc.ty.service.StudentInfoservice;
import com.shzu.jwc.ty.service.SystemConfigservice;

@RequestMapping("/jwc/examinationscoretwo")
@Controller
public class JwcPeExaminationScoreTwocontroller {
	@Autowired
	SystemConfigservice systemConfigservice;
	
	@Autowired
	StudentInfoservice studentInfoservice;
	
	@Autowired
	PeExaminationScoreservice peExaminationScoreservice;
	
	@Autowired
	PeExaminationservice peExaminationservice;
	
	@RequestMapping("/examinationscoretwo.action")
	public String getexaminationscoretwo(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
		Page page=new Page(peExaminationScoreservice.countexaminationscore(2),pn);
		page.setPageSize(20);
		List<PeExaminationAppointment> data=peExaminationScoreservice.getexaminationscore(2,page);
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("data", data);
		model.addAttribute("page",page);
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("date", Semester.getnow());
		model.addAttribute("code", systemConfigservice.getvalue("sportsskillstwo"));
		model.addAttribute("name", peExaminationservice.getskilltest("体育技能二"));
		return "jwc/examinationscoretwo/examinationscoretwo";
	}
	
	@RequestMapping("/selectexaminationscoretwo.action")
	public String selectexaminationscoretwo(@RequestParam("peExaminationId")Integer peExaminationId,@RequestParam(value="pn",defaultValue="1")Integer pn,Model model,HttpSession session) {
		if(peExaminationId==0) {
			Page page=new Page(peExaminationScoreservice.countexaminationscore(2),pn);
			page.setPageSize(20);
			List<PeExaminationAppointment> data=peExaminationScoreservice.getexaminationscore(2,page);
			model.addAttribute("data", data);
			model.addAttribute("page",page);
		}else {
			Page page=new Page(peExaminationScoreservice.countselectexaminationscore(2,peExaminationId),pn);
			page.setPageSize(20);
			List<PeExaminationAppointment> data=peExaminationScoreservice.selectexaminationscore(peExaminationId,2,page);
			model.addAttribute("data", data);
			model.addAttribute("page",page);
		}
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("date", Semester.getnow());
		model.addAttribute("code", systemConfigservice.getvalue("sportsskillstwo"));
		model.addAttribute("name", peExaminationservice.getskilltest("体育技能二"));
		session.setAttribute("peExaminationId",peExaminationId);
		return "jwc/examinationscoretwo/examinationscoretwo";
	}
	
	@RequestMapping("/searchexaminationscoretwo.action")
	public String searchexaminationscoretwo(@RequestParam("userStudentInfoNum")String userStudentInfoNum,@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
		List<PeExaminationAppointment> data=peExaminationScoreservice.searchexaminationscore(userStudentInfoNum, 2);
		model.addAttribute("college", studentInfoservice.getcollege());
		model.addAttribute("data", data);
		model.addAttribute("year", studentInfoservice.getyear());
		model.addAttribute("date", Semester.getnow());
		model.addAttribute("code", systemConfigservice.getvalue("sportsskillstwo"));
		model.addAttribute("name", peExaminationservice.getskilltest("体育技能二"));
		return "jwc/examinationscoretwo/examinationscoretwo";
	}

	@RequestMapping("/updatescore.action")
	@ResponseBody
	public Msg updatescore(@RequestParam("peExaminationAppointmentScore")double peExaminationAppointmentScore, @RequestParam("peExaminationAppointmentId")Integer peExaminationAppointmentId){
		try {
			peExaminationScoreservice.updatescore(peExaminationAppointmentScore,peExaminationAppointmentId);
			return Msg.success("操作成功");
		}catch (Exception ex){
			ex.printStackTrace();
			return Msg.error("操作失败");
		}
	}
	
	@RequestMapping("/export.action")
	public ModelAndView export(@RequestParam("userStudentInfoYear") Integer userStudentInfoYear,@RequestParam("userStudentInfoCollege") String userStudentInfoCollege,@RequestParam("name")Integer peExaminationId,HttpServletResponse response) {
		try {
			List<PeExaminationAppointment> data=peExaminationScoreservice.upselectexaminationscore(userStudentInfoYear, userStudentInfoCollege,peExaminationId,2);
			String code=systemConfigservice.getvalue("sportsskillstwo");
			String title="体育技能考试成绩";
			String[] rowsName=new String[] {"取得学年","取得学期","学号","课程／环节代码","课程类别一","课程类别二","课程类别三","环节类别","修读性质","考核方式","辅修标记","补考标记","特殊情况标记","成绩表现形式","原始成绩"};
			List<Object[]> dataList =new ArrayList<Object[]>();
			Object[] objs=null;
			
			for (int i = 0; i < data.size(); i++) {
				PeExaminationAppointment po =null;
				po=data.get(i);
                objs = new Object[rowsName.length];  
                String[] xq=Semester.getnow().split("-");
                objs[0] = xq[0];
                objs[1] = Integer.parseInt(xq[2])==1?"第一学期":"第二学期";
                objs[2] = po.getUserStudentInfo().getUserStudentInfoNum();  
                objs[3] = code;
                objs[4] = "必修课";
				objs[5]="";
				objs[6]="";
				objs[7]="";
                objs[8] = "初修";  
                objs[9] = "考试";
				if(po.getPeExaminationAppointmentScore()==null)
					objs[12] = "缺考";
				else
					objs[12]=null;
                objs[14] = po.getPeExaminationAppointmentScore();
                dataList.add(objs);  
            }  
			
            ExportExcelUtils ex =new ExportExcelUtils(title, rowsName, dataList,response);  
            ex.exportData();
		}catch(Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/exportTotel.action")
	public ModelAndView exporttotel(HttpServletResponse response){
		try{
			List<PeExaminationAppointment> data=peExaminationScoreservice.getTotelScore(2);
			String[] rowsName=new String[] {"学年","考试名称","学号","姓名","入学时间","所在学院","课程","课程代码","考试成绩"};
			String title="体育技能考试成绩";
			List<Object[]> dataList =new ArrayList<Object[]>();
			Object[] objs=null;
			for (int i = 0; i < data.size(); i++) {
				PeExaminationAppointment po =null;
				po=data.get(i);
				objs = new Object[rowsName.length];
				objs[0] =Semester.getnow();
				objs[1] = po.getPeExamination().getPeExaminationName();
				objs[2] = po.getUserStudentInfo().getUserStudentInfoNum();
				objs[3] = po.getUserStudentInfo().getUserStudentInfoName();
				objs[4] = po.getUserStudentInfo().getUserStudentInfoYear();
				objs[5] = po.getUserStudentInfo().getUserStudentInfoCollege();
				objs[6] = "体育技能二";
				objs[7] = systemConfigservice.getvalue("sportsskillstwo");
				objs[8] = po.getPeExaminationAppointmentScore()==null?"":""+po.getPeExaminationAppointmentScore();
				dataList.add(objs);
			}
			ExportExcelUtils ex =new ExportExcelUtils(title, rowsName, dataList,response);
			ex.exportData();
		}catch (Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
}
